package com.opennews.openplatform.controller;

import com.opennews.openplatform.common.constant.IncomeExpenseTypeConstant;
import com.opennews.openplatform.common.constant.PaymentTypeConstant;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootcore.security.MyClaims;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.AccountGroupService;
import com.opennews.openplatform.service.BankCardService;
import com.opennews.openplatform.service.IncomeExpenseDetailTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/data-initialization")
public class DataInitializationController {
    private final Environment env;
    private final AccountGroupService accountGroupService;
    private final BankCardService bankCardService;
    private final IncomeExpenseDetailTypeService incomeExpenseDetailTypeService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/retrieve-all")
    public ResponseEntity<ApiResponse<Map<String, Object>>> retrieveAll() {
        MyClaims claims = requestIdentityExtractor.getClaims();
        String accountGroupId = claims.accountGroupId;
        String userId = claims.id;
        Map<String, Object> accountGroupInfo = accountGroupService.queryAccountGroupInfo(accountGroupId, userId);
        List<Map<String, Object>> bankCards = bankCardService.queryAllWithBasicInfoByAccountGroup(accountGroupId);
        List<Map<String, Object>> detailTypes = incomeExpenseDetailTypeService.queryByAccountGroup(accountGroupId, null);

        Map<String, Object> incomeExpenseType = Map.of(
                IncomeExpenseTypeConstant.EXPENSE, "支出",
                IncomeExpenseTypeConstant.INCOME, "收入"
        );

        Map<String, Object> paymentType = Map.of(
                PaymentTypeConstant.CASH, "现金",
                PaymentTypeConstant.CREDIT_CARD, "信用卡"
        );

        return httpResponseHelper.getSuccessResponse(Map.of(
                "accountGroupInfo", accountGroupInfo,
                "bankCards", bankCards,
                "detailTypes", detailTypes,
                "incomeExpenseType", incomeExpenseType,
                "paymentType", paymentType,
                "recentSearchListMaxCount", env.getProperty("appConfig.recentSearchListMaxCount", Integer.class),
                "baiduMap", Map.of(
                        "androidKey", env.getProperty("baiduMap.androidKey")
                ),
                "amap", Map.of(
                        "androidKey", env.getProperty("amap.androidKey"),
                        "webApiKey", env.getProperty("amap.webApiKey")
                )
        ));
    }
}
